﻿package fwk.display 
{

	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;

	
	
	public class MovieClipClipping extends MovieClip
	{
		
		private static var _zoneScene:Sprite;
		
		private var _clippingVisible:Boolean = false;
		private var _containerClip:DisplayObjectContainer;
		
		
		
		public function MovieClipClipping()
		{

			
			if ( _zoneScene == null)
			{
				_zoneScene = new Sprite();
				_zoneScene.mouseEnabled = false;
				_zoneScene.mouseChildren = false;
				_zoneScene.graphics.beginFill(0xff0000, 0);
				_zoneScene.graphics.lineTo(0, 10);
				_zoneScene.graphics.lineTo(10, 10);
				_zoneScene.graphics.lineTo(10, 0);
				_zoneScene.graphics.lineTo(0, 0);
				_zoneScene.graphics.endFill();
			}
			
			
			addEventListener(Event.ADDED_TO_STAGE, onAddedScene);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemovedScene);
			

			
		}
		
		//-----------------------------------------------------------------
		//-----------------------------------------------------------------
		//-- Méthodes public overridable
		//-----------------------------------------------------------------
		
		/**
		 * Reprend l'activité du clip (animation, boucle etc...)
		 */
		public function clipping_resume():void 
		{
			
		}
		/**
		 * Stop l'activité du clip (animation, boucle etc...)
		 */
		public function clipping_stop():void 
		{
			
		}
		
		
		
		private function onStageResize(e:Event = null):void 
		{
			_zoneScene.width = this.stage.stageWidth;
			_zoneScene.height = this.stage.stageHeight;
			
		}
		
		
		private function onAddedScene(e:Event):void 
		{
			trace("onAddedScene");
			
			
			_containerClip = this.parent;
			
			this.stage.addEventListener(Event.RESIZE, onStageResize);
			this.stage.addChild(_zoneScene);
			
			//addEventListener("PROPERTY_CHANGED", onRenderClip);
			addEventListener(Event.ENTER_FRAME, onRenderClip);
			
			onStageResize();
			checkLimitScene();
		}
		private function onRemovedScene(e:Event):void 
		{
			this.stage.removeEventListener(Event.RESIZE, onStageResize);
			
			//removeEventListener("PROPERTY_CHANGED", onRenderClip);
			removeEventListener(Event.ENTER_FRAME, onRenderClip);
		}
		
		private function onRenderClip(e:Event):void 
		{
			checkLimitScene();
		}
		
		override public function set x(value:Number):void 
		{
			
			super.x = value;
			dispatchEvent(new Event("PROPERTY_CHANGED"));
		}
		override public function set y(value:Number):void 
		{
			
			super.y = value;
			dispatchEvent(new Event("PROPERTY_CHANGED"));
		}
		
		override public function set scaleX(value:Number):void 
		{
			super.scaleX = value;
			dispatchEvent(new Event("PROPERTY_CHANGED"));
		}
		override public function set scaleY(value:Number):void 
		{
			super.scaleX = value;
			dispatchEvent(new Event("PROPERTY_CHANGED"));
		}
		override public function set width(value:Number):void 
		{
			super.width = value;
			dispatchEvent(new Event("PROPERTY_CHANGED"));
		}
		override public function set height(value:Number):void 
		{
			super.height = value;
			dispatchEvent(new Event("PROPERTY_CHANGED"));
		}
		
		private function checkLimitScene():void 
		{
			
			
			if ( _zoneScene.hitTestObject(this) ) {
				if ( !_clippingVisible)
				{
					trace("display");
					this.visible = true;
					_clippingVisible = true;
					clipping_resume();
				}
			}else {
				if ( _clippingVisible)
				{
					trace("hide");
					this.visible = false;
					_clippingVisible = false;
					clipping_stop();
				}
			}

			
		}
	}
	
}